package com.sy.employee.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sy.employee.dao.po.EmployeePo;
import com.sy.employee.web.fo.ArgsQueryEmployee;
import org.springframework.util.StringUtils;

import java.util.List;

public interface IEmployeeService extends IService<EmployeePo> {

    default List<EmployeePo> listEmployee(ArgsQueryEmployee fo) {
        LambdaQueryWrapper<EmployeePo> wrapper = buildWrapper(fo);
        return this.getBaseMapper().selectList(wrapper);
    }

    default Page<EmployeePo> pageEmployee(ArgsQueryEmployee fo) {
        LambdaQueryWrapper<EmployeePo> wrapper = buildWrapper(fo);
        Page<EmployeePo> page = new Page<>(fo.getCurrentPage(), fo.getPageSize());
        return this.getBaseMapper().selectPage(page, wrapper);
    }

    default LambdaQueryWrapper<EmployeePo> buildWrapper(ArgsQueryEmployee fo) {
        LambdaQueryWrapper<EmployeePo> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(StringUtils.hasLength(fo.getName()), EmployeePo::getName, fo.getName())
                .eq(StringUtils.hasLength(fo.getJob()), EmployeePo::getJob, fo.getJob())
                .eq(fo.getManagerId() != null, EmployeePo::getManagerId, fo.getManagerId())
                .eq(fo.getHireDate() != null, EmployeePo::getHireDate, fo.getHireDate())
                .eq(fo.getSalary() != null, EmployeePo::getSalary, fo.getSalary())
                .eq(fo.getCommission() != null, EmployeePo::getCommission, fo.getSalary())
                .eq(fo.getDepartmentId() != null, EmployeePo::getDepartmentId, fo.getSalary())
        ;

        return wrapper;
    }
}
